package com.towery.dbsrcmanager.git.service;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.api.AddCommand;
import org.eclipse.jgit.api.CommitCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.PushCommand;
import org.eclipse.jgit.api.errors.GitAPIException;

import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.Yaml;
import java.io.*;
import java.net.URL;
import java.util.Map;


/** GIT同步
 * Created by User on 2017/2/8.
 */
@Slf4j
@Service
public class GitSyncService   {


    public  void commitFile(String  filePath ,String fileContent ,String version) throws IOException, GitAPIException {
        Yaml yaml = new Yaml();
        URL url =this.getClass().getClassLoader().getResource("application.yml");
        Map obj = (Map)yaml.load(new FileInputStream(url.getFile()));

        String gitPath =(String)((Map)obj.get("git")).get("root-path");
        File rootDir =new File(gitPath);
        if(!rootDir.exists()){
            Git.init().setDirectory(rootDir).call();
        }

        File newFile =new File(rootDir.getAbsolutePath()+"/"+filePath);
        if(newFile.exists()){
            String s =FileUtils.readFileToString(newFile,"UTF-8");
            if(s.equals(fileContent)){
                return ;
            }
        }






        newFile.getParentFile().mkdirs();

        FileWriter fw = new FileWriter(newFile, false);
        BufferedWriter bw = new BufferedWriter(fw);

        bw.write(fileContent);
        bw.close();
        fw.close();


        //打开git仓库
        Git git = Git.open(rootDir);
        try {
            AddCommand addCmd = git.add();
            addCmd.addFilepattern(filePath);
            addCmd.call();

            CommitCommand commitCmd = git.commit();
            commitCmd.setOnly(filePath);
            commitCmd.setCommitter("admin", "admin@qq.com").setMessage(version).call();


            git.tag().setName(version).call();
        }catch(Exception e){}
    }


    public  void pushCode() throws IOException, GitAPIException {
        Yaml yaml = new Yaml();
        URL url =this.getClass().getClassLoader().getResource("application.yml");
        Map obj = (Map)yaml.load(new FileInputStream(url.getFile()));

        Map gitProp=(Map)obj.get("git");
        String gitPath =(String)(gitProp.get("root-path"));
        File rootDir =new File(gitPath);
        if(!rootDir.exists()){
            Git.init().setDirectory(rootDir).call();
        }

        //打开git仓库
        Git git = Git.open(rootDir);
        try {
            PushCommand pushCmd = git.push();
            pushCmd.setForce(true);
            pushCmd.setRemote((String)gitProp.get("uri"));
            pushCmd.setPushAll();
            pushCmd.setCredentialsProvider( new UsernamePasswordCredentialsProvider( (String)gitProp.get("user"), (String)gitProp.get("password")) );
            pushCmd.call();

        }catch(Exception e){log.error("",e);}
    }

}
